@file:Suppress("unused")

package com.ljj.bitetime.item

import android.os.Parcel
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
 * 菜品对象数据类
 * @param id 菜品ID
 * @param name 菜品名称
 * @param description 菜品描述
 * @param basePrice 菜品基础价格
 * @param category 菜品类别
 * @param imageUrl 菜品图片URL
 * @param options 菜品可选项列表
 */
data class Dish(
    val id: Int,
    val name: String,
    val description: String,
    val basePrice: Double,
    val category: String,
    val imageUrl: String,
    val options: Options = Options(),
    val shopId: Int
) : Parcelable {
    companion object {
        @JvmField
        val CREATOR = object : Parcelable.Creator<Dish> {
            override fun createFromParcel(parcel: Parcel): Dish {
                return Dish(parcel)
            }

            override fun newArray(size: Int): Array<Dish?> {
                return arrayOfNulls(size)
            }
        }
    }

    @Suppress("DEPRECATION")
    private constructor(parcel: Parcel) : this(
        id = parcel.readInt(),
        name = parcel.readString() ?: "",
        description = parcel.readString() ?: "",
        basePrice = parcel.readDouble(),
        category = parcel.readString() ?: "",
        imageUrl = parcel.readString() ?: "",
        options = parcel.readParcelable(Options::class.java.classLoader) ?: Options(),
        shopId = parcel.readInt()
    )

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeInt(id)
        dest.writeString(name)
        dest.writeString(description)
        dest.writeDouble(basePrice)
        dest.writeString(category)
        dest.writeString(imageUrl)
        dest.writeParcelable(options, flags)
        dest.writeInt(shopId)
    }

    override fun describeContents(): Int = 0
}

/**
 * 菜品自定义选项数据类
 */
@Parcelize
data class Options(
    val portionSize: PortionSize? = null,
    val spicyLevel: SpicyLevel? = null,
    val sugarLevel: SugarLevel? = null,
    val iceLevel: IceLevel? = null,
    val toppings: List<Topping>? = null,
    val dietaryRestrictions: List<DietaryRestriction>? = null
) : Parcelable {
    /**
     * 检查菜品对象是否没有额外选项
     * @return true or false
     */
    fun isEmpty() : Boolean {
        return portionSize == null &&
                spicyLevel == null &&
                sugarLevel == null &&
                iceLevel == null &&
                toppings == null &&
                dietaryRestrictions == null
    }

    fun joinToString(separator: String): String {
        val parts = mutableListOf<String>()
        portionSize?.let { parts.add(it.description) }
        spicyLevel?.let { parts.add(it.description) }
        sugarLevel?.let { parts.add(it.description) }
        iceLevel?.let { parts.add(it.description) }
        toppings?.let { parts.addAll(it.map { topping -> topping.description }) }
        dietaryRestrictions?.let { parts.addAll(it.map { restriction -> restriction.description }) }
        return parts.joinToString(separator)
    }
}

/**
 * 分量枚举
 * @param description 分量描述
 * @param extraPrice 额外价格
 */
@Parcelize
enum class PortionSize(val description: String, var extraPrice: Double) : Parcelable {
    SMALL("小份", 0.0),
    MEDIUM("中份", 2.0),
    LARGE("大份", 4.0)
}

/**
 * 辣度枚举
 * @param description 辣度描述
 */
@Parcelize
enum class SpicyLevel(val description: String) : Parcelable {
    NONE("不辣"),
    MILD("微辣"),
    MEDIUM("中辣"),
    HOT("重辣")
}

/**
 * 糖度枚举
 * @param description 糖度描述
 */
@Parcelize
enum class SugarLevel(val description: String) : Parcelable {
    NONE("不额外加糖"),
    THREE("3分糖"),
    FIVE("5分糖"),
    SEVEN("7分糖"),
    NORMAL("正常糖")
}

/**
 * 冰度枚举
 * @param description 冰度描述
 */
@Parcelize
enum class IceLevel(val description: String) : Parcelable {
    HOT("温热"),
    NONE("去冰"),
    LOW("少冰"),
    MEDIUM("半冰"),
    NORMAL("正常冰")
}



/**
 * 加料枚举
 * @param description 加料描述
 * @param extraPrice 加料额外价格
 */
@Parcelize
enum class Topping(val description: String, val extraPrice: Double) : Parcelable {
    PEARL("珍珠", 2.0),
    RED_BEAN("红豆", 1.5),
    PUDDING("布丁", 3.0),
    CHEESE_FOAM("奶盖", 4.0),
    GRASS_JELLY("仙草", 2.0),
    ALOE_VERA("芦荟", 2.5),
    COCONUT_JELLY("椰果", 1.5),
    OREO("奥利奥", 2.5)
}

/**
 * 忌口枚举
 * @param description restriction 描述
 */
@Parcelize
enum class DietaryRestriction(val description: String) : Parcelable {
    NONE("无特殊要求"),
    NO_GREEN_ONION("去葱"),
    NO_GARLIC("去蒜"),
    NO_GINGER("去姜"),
    NO_PARSLEY("去香菜")
}